package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.Achieve;
import com.yantong.pojo.AchieveFlow;
import com.yantong.pojo.AchieveFlowExample;
import com.yantong.mapper.AchieveFlowMapper;
import com.yantong.service.AchieveFlowService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kanghaixin
 * @date 2018/9/20.13:44
 */

@Service
public class AchieveServiceImpl implements AchieveFlowService {
    @Autowired
    private AchieveFlowMapper achieveFlowMapper;



    @Override
    public PageUtils<AchieveFlow> listAchieveFlows(Long centreId,Long teamId,Date start, Date end, String name, int offset, int limit) {
        PageUtils<AchieveFlow> pageUtils = new PageUtils<>();
        Map<String,Object> params = new HashMap<>(4);
        if (start != null) {
            params.put("start", start);
        }
        if (end != null) {
            params.put("end", end);
        }
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        if (teamId != null) {
            params.put("teamId", teamId);
        }
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        int total = achieveFlowMapper.countByParams(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(achieveFlowMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public List<AchieveFlow> listAchieveFlows(Long centreId,Long teamId, Date start, Date end, String name) {
        Map<String,Object> params = new HashMap<>(4);
        if (start != null) {
            params.put("start", start);
        }
        if (end != null) {
            params.put("end", end);
        }
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        if (teamId != null) {
            params.put("teamId", teamId);
        }
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        return achieveFlowMapper.listByParams(params);
    }

    @Override
    public AchieveFlow getByMidAndDate(Long mid, Date date) {
        if (mid == null || date == null) {
            return null;
        }
        AchieveFlowExample example = new AchieveFlowExample();
        AchieveFlowExample.Criteria criteria = example.createCriteria();
        criteria.andMidEqualTo(mid)
                .andAchieveDateEqualTo(date);

        example.setDistinct(true);
        example.setOrderByClause("create_time desc");
        List<AchieveFlow> list = achieveFlowMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<Achieve> countAchieve(Long centreId, Long pmid, Date start, Date end, int offset,int limit) {
        Map<String,Object> params = new HashMap<>(4);
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        if (pmid != null) {
            params.put("pmid", pmid);
        }
        if (start != null) {
            params.put("start", start);
        }
        if (end != null){
            params.put("end", end);
        }
        return achieveFlowMapper.countAchieve(params, new RowBounds(offset, limit));
    }
    
    @Override
    public PageUtils<AchieveFlow> listAchieveFlowsToMid(AchieveFlow achieveFlow, int offset, int limit) {
        PageUtils<AchieveFlow> pageUtils = new PageUtils<>();
        Map<String,Object> params = new HashMap<>(4);
        if (achieveFlow.getMid() != null) {
            params.put("mid", achieveFlow.getMid());
        }
        if (achieveFlow.getCentreId() != null) {
            params.put("centreId", achieveFlow.getCentreId());
        }
        int total = achieveFlowMapper.countByParams(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(achieveFlowMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }
}
